package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Min;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    /**
     * 加载文章
     * @param dto
     * @param type 1为加载更多  2为加载最新
     * @return
     */
    @Override
    public List<ApArticle> loadArticleList(ArticleHomeDto dto, Short type) {
        //首先校验前端传来的数据
        Integer size = dto.getSize();
        if(size == null && size == null){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);//默认查询全部
        }

        if(dto.getMaxBehotTime()==null)
            dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null)
            dto.setMinBehotTime(new Date());
        //校验数据完成后进行业务处理
        //查询数据
        List<ApArticle> articleList = this.apArticleMapper.loadArticleList(dto, type);

        return articleList;
    }
}
